<template>
  <el-container class="home_container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt />
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="exitLogin">退出登录</el-button>
    </el-header>
    <el-container>
      <!-- 侧边导航栏 -->
      <el-aside :width="isFolding ? '64px' : '200px'">
        <div class="foldingButton" @click="changeFolding">|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened="true" :collapse='isFolding' :collapse-transition="false" :router='true' :default-active='activePath'>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- d'd'd -->
            <el-menu-item :index="'/' + children.path" v-for="children in item.children" :key="children.id" @click="changeActive('/' + children.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ children.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主页 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-lock_fill',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isFolding: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    //   退出登录
    exitLogin() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      this.menuList = res.data
    },
    changeFolding() {
      this.isFolding = !this.isFolding
    },
    changeActive(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #b3c0d1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    color: white;
    font-size: 20px;
    align-items: center;
    span {
      margin-left: 10px;
    }
    img {
      width: auto;
      height: 60px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.iconfont {
    margin-right: 10px;
}
.el-main {
  background-color: #e9eef3;
}
.foldingButton {
    background: #4a5064;
    text-align: center;
    // 设置字间距
    letter-spacing: 0.2em;
    font-size: 12px;
    line-height: 24px;
    color: white;
    // 鼠标显示小手
    cursor: pointer;
}
</style>
